<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

/**
 * get_article_url()
 *
 * @return
 */
if ( ! function_exists('get_article_url'))
{
	function get_article_url($all_article_list,$template,$item_id){
		$url = '';
		foreach($all_article_list as $all_article){
			if($all_article->template == $template && $all_article->item_id == $item_id){
				$url = PATH_URL.$all_article->link;
				break;
			}
		}
		return $url;
	}
}
if ( ! function_exists('get_article_url'))
{
	function resize_image($source_dir , $width, $height=''){
		require_once 'phpthumb/ThumbLib.inc.php';  
		$thumb = PhpThumbFactory::create($source_dir);  
		$thumb->resize($width, $height);
		$thumb->save($source_dir);
	}
}

/*BEGIN: SEARCH ARTICLE*/
if ( ! function_exists('normalize_search_content'))
{
	function normalize_search_content($content){
		$content = html2text($content);
		$content = trim($content);
		// $content = v2e_special($content);
		return $content;
	}
}

if(!function_exists('merge_content_for_searching')){
    function merge_content_for_searching(&$content,$additional_content){
		$additional_content = normalize_search_content($additional_content);
		$content .= CONTENT_SEPARATOR.$additional_content;
	}
}

if(!function_exists('activate_search_for_list')){
	function activate_search_for_list($CI,$template=''){
		$field_id = 'item_id';
		switch($template){
			case '';
				$field_id = 'id';
				$table = ARTICLE_TABLE;
				break;
			case TEMPLATE_PRODUCT_DETAIL;
				$table = PRODUCT_TABLE;
				break;
			case TEMPLATE_PRODUCT_CATEGORY;
				$table = PRODUCT_CATEGORY_TABLE;
				break;
		}
		$obj_list = $CI->model->fetch('*',$table,"",'id','ASC');
		foreach($obj_list as $obj){
			$searched_article = activate_search_for_obj($obj);
			$CI->model->update(ARTICLE_TABLE,$searched_article,"template = '{$template}' AND {$field_id} = {$obj->id}");
		}
	}
}
if ( ! function_exists('html2text'))
{
	function html2text($html)
	{
		// Include the class definition file.
		require_once('class.html2text.php');

		// Instantiate a new instance of the class. Passing the string
		// variable automatically loads the HTML for you.
		$h2t =& new html2text($html);

		// Simply call the get_text() method for the class to convert
		// the HTML to the plain text. Store it into the variable.
		return $h2t->get_text();
	}
}

if(!function_exists('trim_content')){
	function trim_content($str,$len,$more){
		if ($str=='' || $str==NULL) return $str;
		if (is_array($str)) return $str;
		$str_html= html2text($str);
		$str = trim($str_html);
		if (strlen($str) <= $len) return $str;
		$str = substr($str,0,$len);
		if ($str != '') {
		if (!substr_count($str," ")) {
		if ($more) $str .= " ...";
		return $str;
		}
		while(strlen($str) && ($str[strlen($str)-1] != " ")) $str = substr($str,0,-1);
			$str = substr($str,0,-1);
		if ($more) $str .= " ......";
		}
		return $str;
	} 
}

if(!function_exists('activate_search_for_obj')){
    function activate_search_for_obj($obj){
		$search_content = '';
		if(!empty($obj->name)){
			merge_content_for_searching($search_content,$obj->name);
		}
		if(!empty($obj->title)){
			merge_content_for_searching($search_content,$obj->title);
		}
		merge_content_for_searching($search_content,$obj->description);
		merge_content_for_searching($search_content,$obj->content);
		$searched_article['search_content'] = $search_content;
		$searched_article['search_content_clean'] = v2e_special($search_content);
		$searched_article['search_image'] = $obj->image;
		return $searched_article;
	}
}

if(!function_exists('find_exactly_keyword_list_from_normalized_string'))
{
    function find_exactly_keyword_list_from_normalized_string($keyword, $content)
    {
		$keyword_token_list = split(' ',$keyword);//pr($keyword_token_list);
		$content_token_list = split(' ',$content);//pr($content_token_list);
		$exactly_keyword_list = array();
		$temp_keyword = '';
		$count = 0;
		if(!empty($content_token_list) && !empty($keyword_token_list)){
			foreach($content_token_list as $content_token){
				for($i=$count;$i<count($keyword_token_list);$i++){
					$keyword_token = $keyword_token_list[$i];
					$return_token = '';
					$ignore_prefix = $count==0;
					$ignore_suffix = $count==count($keyword_token_list)-1;
					// Dieu kien equal:
					// $i==$count
					// compare_normalized_word -> true (co the k trung hoan toan do ignore_prefix hoac ignore_suffix gay ra)
					if($i==$count && compare_normalized_word($keyword_token,$content_token,$return_token,$ignore_prefix,$ignore_suffix)){
						$temp_keyword .= $return_token.' ';
						$count++;
						//pr('$temp_keyword:'.$temp_keyword);pr('$count++:'.$count);
						if($count == count($keyword_token_list)){
							$exactly_keyword_list[] = trim($temp_keyword);//pr('$exactly_keyword_list:');pr($exactly_keyword_list);
							$temp_keyword = '';
							$count = 0;
						}
						break;
					} else {//pr('reset');
						$temp_keyword = '';
						$count = 0;
					}
				}
			}
		}
		$exactly_keyword_list = array_unique($exactly_keyword_list);
		return $exactly_keyword_list;
	}
}

if(!function_exists('compare_normalized_word'))
{
    function compare_normalized_word($keyword_token,$content_token,&$return_token,$ignore_prefix=false,$ignore_suffix=false)
    {	
		$is_equal = false;
		$return_token = '';
		$normalized_keyword_token = strtolower(v2e_special($keyword_token));
		$normalized_content_token = strtolower(v2e_special($content_token));
		//pr('$normalized_keyword_token:'.$normalized_keyword_token);pr('$normalized_content_token:'.$normalized_content_token);
		if($normalized_keyword_token == $normalized_content_token){//pr('is_equal');
			$is_equal = true;
			$return_token = $content_token;
		} else {
			$keyword_token_char_list = string_to_array($keyword_token);//pr($keyword_token_char_list);
			$content_token_char_list = string_to_array($content_token);//pr($content_token_char_list);
			$keyword_token_length = count($keyword_token_char_list);
			$content_token_length = count($content_token_char_list);
			//pr('$keyword_token_length:'.$keyword_token_length);pr('$content_token_length:'.$content_token_length);
			if($keyword_token_length < $content_token_length){// keyword phai nho hon content
				$pos = strpos($normalized_content_token,$normalized_keyword_token);//pr('$pos:'.$pos);
				if($pos !== false){//Important for algorithm
					if($ignore_prefix && $pos == (strlen($normalized_content_token)-strlen($normalized_keyword_token))){
						$return_token = mb_substr($content_token,$pos,strlen($content_token));
						$is_equal = true;
						//pr('$ignore_prefix:'.$return_token);
					}
					if($ignore_suffix && $pos == 0){
						$return_token = mb_substr($content_token,0,strlen($keyword_token));
						//$return_token = substr($content_token,0,strlen($keyword_token));
						$is_equal = true;
						//pr('$ignore_suffix:'.$return_token);
					}
				}
			}
		}
		return $is_equal;
	}
}
if(!function_exists('string_to_array'))
{
    function string_to_array($str)
    {
		$result = array();
		preg_match_all('/./u', $str, $result);
		if(!empty($result[0])){
			$result = $result[0];
		} else {
			$result = null;
		}
		return $result;
	}
}
/*END: SEARCH ARTICLE*/